﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;

namespace SuperX.Common.Log
{
    // 
    public static class NativeMethods
    {
        // 
        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(2)]
        internal static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

        // 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(2)]
        internal static extern bool CloseHandle(IntPtr handle);

        // 
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(2)]
        internal static extern bool DuplicateToken(IntPtr existingTokenHandle, int impersonationLevel, out IntPtr duplicateTokenHandle);

        // 
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        internal static extern void OutputDebugString(string message);

        // 
        [DllImport("kernel32.dll")]
        [return: MarshalAs(2)]
        internal static extern bool QueryPerformanceCounter(out ulong lpPerformanceCount);

        // 
        [DllImport("kernel32.dll")]
        [return: MarshalAs(2)]
        internal static extern bool QueryPerformanceFrequency(out ulong lpPerformanceFrequency);

        // 
        [DllImport("kernel32.dll")]
        internal static extern int GetCurrentProcessId();

        // 
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        internal static extern uint GetModuleFileName([In] IntPtr hModule, [Out] StringBuilder lpFilename, [MarshalAs(8)][In] int nSize);

        // 
        public static string GetAppInfo(AppDomain appDomain)
        {
            int num = PlatformDetector.IsWin32 ? NativeMethods.GetCurrentProcessId() : Process.GetCurrentProcess().Id;
            return string.Format(CultureInfo.InvariantCulture, "{0}({1})", appDomain.FriendlyName, num);
        }
    }
}
